<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script>
   /* 
   创建对象的几种方式：
   1.字面量模式：
   2.工厂模式
   3.构造函数创建对象
   4.class
    */
    //1.字面量模式创建对象
    var Student={
            name:456,
            age:50,
            like:function(){
              console.log(this.name+this.age+"岁，喜欢"+like);
            }
        } 
      
  //2.工厂模式闯将对象
  var Student=function(name,age,like){
        var Student={
            name:name,
            age:age,
            like:function(){
              console.log(this.name+this.age+"岁，喜欢"+like);
            }
        } 
        return Student;
    }
   console.log(Student("小王",20,"rap").like());
   console.log(Student("老王",40,"LSP").like());
  //3.构造函数创建对象,Object浏览器自带
  function boy(name,age,like){
    this.name=name,
    this.age=age,
    this.like=function(){
      console.log(this.name+"喜欢"+like);
    }
    console.log(this);
  }
  //加上new,创建了新的对象，把函数内部的this指向此对象，没加上之前指向的是window
  var b1=new boy("jason",15,"看片");
  console.log(b1);
  var b2=new boy("tom",05,"写代码");
  console.log(b2);

  //4.class创建对象{ES6新出的，允许class创建}
  class Cat{
    //构造函数
    constructor(name,color,like){
        this.name=name,
        this.color=color,
        this.like=function(){
          console.log(this.name+"喜欢"+like);
        }
    }  
  }
  var c1=new Cat("波斯猫","白色","卖萌");
  console.log(c1);
  var c2=new Cat("黑猫","黑色","捉老鼠");
  console.log(c2);


  </script>
</head>
<body>
  
</body>
</html>